package com.example.serviceA.dao;

import com.example.serviceA.req.TransferOutReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AccountMapper {

    @Update("update account " +
            "   set amount = amount - #{req.amount}, " +
            "       frozen_amount = frozen_amount + #{req.amount} " +
            " where id = #{req.accountId} " +
            "   and amount - #{req.amount} >= 0 ")
    int transferOut(@Param("req") TransferOutReq req);

    @Update("update account set frozen_amount = frozen_amount - #{req.amount} " +
            " where id = #{req.accountId} ")
    void confirmTransferOut(@Param("req") TransferOutReq req);

    @Update("update account " +
            "   set amount = amount + #{req.amount}, " +
            "       frozen_amount = frozen_amount - #{req.amount} " +
            " where id = #{req.accountId} ")
    void cancelTransferOut(@Param("req") TransferOutReq req);

}
